/*
package com.ssmk.module.shoppingcar.presenter;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.example.retrofitlibrary.BaseObserBean2;
import com.example.retrofitlibrary.RetrofitClientUtlis;
import com.ssmk.R;
import com.ssmk.base.BasePresenter;
import com.ssmk.base.BaseView;
import com.ssmk.base.Constant;
import com.ssmk.module.shoppingcar.adapter.AllOrderAdapter;
import com.ssmk.module.shoppingcar.bean.OrderAllBean;
import com.ssmk.module.shoppingcar.bean.SaleAfterBean;
import com.ssmk.module.shoppingcar.contract.AllOrderContract;
import com.ssmk.url.Url;

import java.util.HashMap;
import java.util.Map;

public class AllOrderPresenter implements BasePresenter{

    private int mOrder;
    private AllOrderAdapter mAdapter;
    private int mPageNo;
    private String mOrderId;
    private BaseView baseView;
    private Context context;
    private String mTab;
    Map<String, String> mapHeader = new HashMap<>();
    private String mCancelOrderFlag;

    public AllOrderPresenter(AllOrderContract.View view) {
        mapHeader.put("token", Constant.User.TOKEN);
        baseView = view;
    }


    public AllOrderPresenter(Context context, BaseView baseView, String tab, int pageNo) {
        this.baseView = baseView;
        this.context = context;
        this.mTab = tab;
        this.mPageNo = pageNo;
    }

   //取消-删除-订单
    public AllOrderPresenter(AllOrderAdapter adapter, Context context, String orderId, int order, String flag) {
        this.mAdapter = adapter;
        this.context = context;
        this.mOrderId = orderId;
        this.mOrder = order;
        this.mCancelOrderFlag = flag;
    }


    @Override
    public void SetDataModel(Object o) {

    }

    @Override
    public void GetDataModel() {
        Map<String, String> allMap = new HashMap<>();//全部
        Map<String, String> waitPayMap = new HashMap<>();//待付款
        Map<String, String> waitSendMap = new HashMap<>();//待发货
        Map<String, String> waitGetMap = new HashMap<>();//待收货
        Map<String, Integer> saleAfterMap = new HashMap<>();//售后
        mapHeader.put("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjI1MiwicHdkIjoiZTEwYWRjMzk0OWJhNTlhYmJlNTZlMDU3ZjIwZjg4M2UiLCJpYXQiOjE1MDA0MzYyNzg1MDIsImV4dCI6MTUwMTA0MTA3ODUwMn0.ws2972b0tfqUWbiluxEEtv_2mbAjZzk3k9lTFxyHIvA");

        if (!TextUtils.isEmpty(mTab)) {
            if (mTab.equals(context.getString(R.string.qubu))) {
                allMap.put("orderStatus", "66");
                allMap.put("pageNo", mPageNo + "");
                //allMap.put("payStatus",mPayStatus);
                requestOrderDate(allMap, mapHeader);
            } else if (mTab.equals(context.getString(R.string.waitPay))) {
                waitPayMap.put("orderStatus", "66");
                waitPayMap.put("payStatus", String .valueOf(0));
                waitPayMap.put("pageNo", mPageNo + "");
                requestOrderDate(waitPayMap, mapHeader);
            } else if (mTab.equals(context.getString(R.string.waitSend))) {
                waitSendMap.put("orderStatus", "0");
                waitSendMap.put("payStatus",String .valueOf(1));
                waitSendMap.put("pageNo", mPageNo + "");
                requestOrderDate(waitSendMap, mapHeader);
            } else if (mTab.equals(context.getString(R.string.alreadySend))) {
                waitGetMap.put("orderStatus", "1");
                waitSendMap.put("payStatus",String .valueOf(1));
                waitGetMap.put("pageNo", mPageNo + "");
                requestOrderDate(waitGetMap, mapHeader);
            } else if (mTab.equals(context.getString(R.string.saleAfter))) {
                saleAfterMap.put("pageNo", mPageNo);
                saleAfterMap.put("pageSize", 20);
                //requestSaleAfterDate(saleAfterMap, mapHeader);
            }
        }

        Map<String, String> cancelMap = new HashMap<>();//取消Order
        Map<String, String> deleteMap = new HashMap<>();//删除Order
        //取消
        if (!TextUtils.isEmpty(mOrderId) && mOrder == 0) {
            cancelMap.put("orderId", mOrderId);
            requestCancelOrder(Url.CANCELORDER, cancelMap, mapHeader);
        }
        //删除
        if (!TextUtils.isEmpty(mOrderId) && mOrder == 1) {
            LogUtils.e("PresenterOrderId","小明："+mOrderId);
            deleteMap.put("orderId", mOrderId);
            requestDeleteOrder(Url.DELETEORDER, deleteMap, mapHeader);
        }
    }





    private void requestOrderDate(Map<String, String> map, Map<String, String> mapHeader) {
        RetrofitClientUtlis.getInstance(context, Url.BASEORDER, mapHeader)
                .createBaseApi()
                .post2(Url.ALLORDER_LINK, map, new BaseObserBean2<OrderAllBean>(context, OrderAllBean.class) {
                    @Override
                    public void Next(OrderAllBean data) {
                        if (data != null) {
                            LogUtils.e("====",data.toString());
                            baseView.UpData(data);
                        } else {
                            Toast.makeText(context, "请求数据失败！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    //售后
    private void requestSaleAfterDate(Map<String, Integer> map, Map<String, String> header) {
        RetrofitClientUtlis.getInstance(context, Url.BASESALEAFTER, header)
                .createBaseApi()
                .postSaleAfter(Url.SALEAFTER, map, new BaseObserBean2<SaleAfterBean>(context, SaleAfterBean.class) {

                    @Override
                    public void Next(SaleAfterBean data) {
                        if (data != null) {
                            baseView.UpData(data);
                        }
                    }
                });
    }

    //取消order
    private void requestCancelOrder(String url, Map<String, String> map, Map<String, String> mapHeader) {
        RetrofitClientUtlis.getInstance(context, Url.BASEORDER, mapHeader)
                .createBaseApi()
                .post2(url, map, new BaseObserBean2<OrderAllBean>(context, OrderAllBean.class) {
                    @Override
                    public void Next(OrderAllBean data) {
                        if (data != null) {
                            mAdapter.getCancelDeleteDate(data, mOrderId, mCancelOrderFlag);
                        }else {
                            Toast.makeText(context,data.toString(),Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    //删除订单
    private void requestDeleteOrder(String url, Map<String, String> map, Map<String, String> mapHeader) {
        RetrofitClientUtlis.getInstance(context, Url.BASEORDER, mapHeader)
                .createBaseApi()
                .post2(url, map, new BaseObserBean2<OrderAllBean>(context, OrderAllBean.class) {
                    @Override
                    public void Next(OrderAllBean data) {
                        if (data != null) {
                            mAdapter.getDeleteDate(data, mOrderId);
                        }
                    }
                });
    }

}
*/
package com.ssmk.module.shoppingcar.presenter;

import android.text.TextUtils;

import com.example.retrofitlibrary.BaseObserBean2;
import com.example.retrofitlibrary.RetrofitClientUtlis;
import com.ssmk.appliaction.MyAppliacton;
import com.ssmk.base.Constant;
import com.ssmk.module.shoppingcar.bean.OrderAllBean;
import com.ssmk.module.shoppingcar.bean.SaleAfterBean;
import com.ssmk.module.shoppingcar.contract.AllOrderContract;
import com.ssmk.url.Url;
import com.ssmk.utlis.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AllOrderPresenter implements AllOrderContract.Presenter {

    private AllOrderContract.View baseView;

    private Map<String, String> mapHeader = new HashMap<>();

    private String mOrderId;

    private final String CANCELORDER = "cancleOrder.do"; //取消ORDER

    private final String DELETEORDER = "deleteOrder.do";

    private String mCancelOrderFlag;

    public AllOrderPresenter(AllOrderContract.View view) {
        mapHeader.put("token", Constant.User.TOKEN);
        baseView = view;
    }

    @Override
    public void SetDataModel(Object o) {

    }

    @Override
    public void queryAllOrder(int page) {
        Logger.i("queryAllOrder  pageNo  " + page);
        Map<String, String> allMap = new HashMap<>();//全部
        allMap.put("orderStatus", "66");
        allMap.put("pageNo", page + "");
        requestOrderDate(allMap, mapHeader);
    }

    @Override
    public void waitPayOrder(int page) {
        Logger.i("waitPayOrder  pageNo  " + page);
        Map<String, String> waitPayMap = new HashMap<>();//待付款
        waitPayMap.put("payStatus", "0");
        waitPayMap.put("pageNo", page + "");
        requestOrderDate(waitPayMap, mapHeader);
    }

    @Override
    public void waitSendOrder(int page) {
        Logger.i("waitSendOrder  pageNo  " + page);
        Map<String, String> waitSendMap = new HashMap<>();//待发货
        waitSendMap.put("orderStatus", "0");
        waitSendMap.put("pageNo", page + "");
        requestOrderDate(waitSendMap, mapHeader);
    }

    @Override
    public void waitGetOrder(int page) {
        Logger.i("waitGetOrder  pageNo  " + page);
        Map<String, String> waitGetMap = new HashMap<>();//待收货
        waitGetMap.put("orderStatus", "1");
        waitGetMap.put("pageNo", page + "");
        requestOrderDate(waitGetMap, mapHeader);
    }

    @Override
    public void saleAfterOrder(int page) {
        Logger.i("saleAfterOrder  pageNo  " + page);
        Map<String, Integer> saleAfterMap = new HashMap<>();//售后
        saleAfterMap.put("pageNo", page);
        saleAfterMap.put("pageSize", 20);
        requestSaleAfterDate(saleAfterMap, mapHeader);
    }

    @Override
    public void stop() {
        baseView = null;
    }

    @Override
    public void GetDataModel() {

    }

    private void requestOrderDate(Map<String, String> map, Map<String, String> mapHeader) {
        RetrofitClientUtlis.getInstance(MyAppliacton.getContext(), Url.BASEORDER, mapHeader)
                .createBaseApi()
                .post2(Url.ALLORDER_LINK, map, new BaseObserBean2<OrderAllBean>(MyAppliacton.getContext(), OrderAllBean.class) {
                    @Override
                    public void Next(OrderAllBean data) {
                        if (null == baseView)
                            return;
                        if (null == data) {
                            baseView.showToast("请求数据失败");
                            return;
                        }
                        //TODO 页面完成注释掉
                        Logger.e("**********************************************");
                        List<OrderAllBean.DataBean> data1 = data.getData();
                        for (int i = 0; i < data1.size(); i++) {
                            Logger.i("订单id  " + data1.get(i).getOrderId());
                        }
                        Logger.e("**********************************************");
                        baseView.UpData(data);
                    }
                });
    }

    //售后
    private void requestSaleAfterDate(Map<String, Integer> map, Map<String, String> header) {
        RetrofitClientUtlis.getInstance(MyAppliacton.getContext(), Url.BASESALEAFTER, header)
                .createBaseApi()
                .postSaleAfter(Url.SALEAFTER, map, new BaseObserBean2<SaleAfterBean>(MyAppliacton.getContext(), SaleAfterBean.class) {
                    @Override
                    public void Next(SaleAfterBean data) {
                        if (null == baseView)
                            return;
                        if (null == data)
                            return;
                        baseView.UpData(data);
                    }
                });
    }

    /**
     * 取消订单
     *
     * @param orderId
     * @param flag
     */
    @Override
    public void cancelOrder(String orderId, String flag) {
        mCancelOrderFlag = flag;
        mOrderId = orderId;
        if (TextUtils.isEmpty(mOrderId))
            return;
        Map<String, String> cancelMap = new HashMap<>();
        cancelMap.put("orderId", mOrderId);
        requestCancelOrder(CANCELORDER, cancelMap, mapHeader);
    }

    /**
     * 删除订单
     *
     * @param orderId
     */
    @Override
    public void deleteOrder(String orderId) {
        mOrderId = orderId;
        if (TextUtils.isEmpty(mOrderId))
            return;
        Map<String, String> deleteMap = new HashMap<>();
        deleteMap.put("orderId", mOrderId);
        requestDeleteOrder(DELETEORDER, deleteMap, mapHeader);
    }


    //取消order --delete
    private void requestCancelOrder(String url, Map<String, String> map, Map<String, String> mapHeader) {
        RetrofitClientUtlis.getInstance(MyAppliacton.getContext(), Url.BASEORDER, mapHeader)
                .createBaseApi()
                .post2(url, map, new BaseObserBean2<OrderAllBean>(MyAppliacton.getContext(), OrderAllBean.class) {
                    @Override
                    public void Next(OrderAllBean data) {
                        if (null == baseView)
                            return;
                        if (data.getCode() == 200) {
                            baseView.showToast(data.getMsg());
                            baseView.cancelOrderResult(mOrderId, mCancelOrderFlag);
                        } else {
                            //TODO 取消失败的时候可以给用户一些提示
                        }
                    }
                });
    }

    //删除订单
    private void requestDeleteOrder(String url, Map<String, String> map, Map<String, String> mapHeader) {
        RetrofitClientUtlis.getInstance(MyAppliacton.getContext(), Url.BASEORDER, mapHeader)
                .createBaseApi()
                .post2(url, map, new BaseObserBean2<OrderAllBean>(MyAppliacton.getContext(), OrderAllBean.class) {
                    @Override
                    public void Next(OrderAllBean data) {
                        Logger.i("删除订单接口  " + data);
                        if (null == baseView)
                            return;
                        if (data.getCode() == 200) {
                            baseView.showToast(data.getMsg());
                            baseView.deleteOrderResult(mOrderId);
                        } else {
                            //TODO 取消失败的时候可以给用户一些提示
                        }
                    }
                });
    }


}
